<template>
	<gui-page ref="guiPage" :customHeader="true">
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('consumo.salesOutboundReturn')"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="distributed">
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text"
							style="width:300rpx">{{ $t('smashRemoval.letterOfAdvice') }}：</text>
						<view class="gui-form-body gui-text-right">
							<input type="text" class="gui-form-input gui-text-right" v-model="fromData.receiptCode"
								:placeholder="$t('distributedEnter.placeholderText')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text">{{ $t('distributedEnter.date') }}：</text>
						<view class="gui-form-body gui-text-right" @click="dateChoose">
							<view class="gui-form-input gui-text-right" v-if="fromData.chooseTime.length">
								<text>{{ fromData.chooseTime[0] }}</text> {{ fromData.chooseTime[1] ? '-' : '' }}
								<text>{{ fromData.chooseTime[1] }}</text>
							</view>
							<input v-else type="button" class="gui-form-input gui-text-right"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text"
							style="width:400rpx">{{ $t('smashRemoval.completionStatus') }}：</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<superweiCombox style="text-align:right;" :candidates="data.selectList"
								:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="name"
								@select="select" v-model="fromData.selectIdText" :defaultValue="$t('smashRemoval.all')">
							</superweiCombox>
						</view>
					</view>
					<view class="gui-form-item gui-border-b gui-flex gui-justify-content-center">
						<view class="gui-flex gui-space-between gui-align-items-center"
							style="width: 70%;height: 100rpx;">
							<button type="default" class="gui-button gui-button-mini gui-bg-black4 gui-noborder"
								style="width:200rpx;" @click="reset">
								<text
									class="gui-block gui-color-black gui-button-text-mini gui-icons">{{ $t('distributedEnter.reset') }}</text>
							</button>
							<button type="default" class="gui-button gui-button-mini gui-bg-blue gui-noborder"
								style="width:200rpx;" @click="search">
								<text
									class="gui-block gui-color-white gui-button-text-mini gui-icons">{{ $t('distributedEnter.search') }}</text>
							</button>
						</view>
					</view>
				</view>
				<!-- 筛选按钮 ,做完成和未完成的筛选-->

				<!-- 底部显示区域 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolower">
					<view style="height: 15rpx"></view>
					<view class="card-boxCustom" v-for="(x, i) in data.list" @click="goPage(x)" :key="i">
						<view class="card-head">
							<text>{{ $t('smashRemoval.letterOfAdvice') }}：{{ x.dnCode }}</text>
							<gui-tags v-if="x.dnStatus == 'notSend'" :text="$t('smashRemoval.unshipped')"
								:customClass="['gui-transparent-bg', 'gui-color-blue']" :width="80" :lineHeight="2.5"
								:size="12" borderColor="#008AFF"></gui-tags>
							<gui-tags v-else :text="$t('smashRemoval.delivered')"
								:customClass="['gui-transparent-bg', 'gui-color-green']" :width="80" :lineHeight="2.5"
								:size="12" borderColor="#39B55A"></gui-tags>
						</view>
						<view class="card-body" style="padding-bottom: 15rpx;">
							<!-- 展开按钮 -->
							<view class="card-rotating card-rotating-top"
								@click.stop="() => data.list[i].openBool = !data.list[i].openBool"
								v-if="x.wmsDnDS.length > 0">
								<uni-icons v-if="!data.list[i].openBool" type="bottom" color="#9C9C9C"
									size="20"></uni-icons>
								<uni-icons v-if="data.list[i].openBool" type="top" color="#9C9C9C"
									size="20"></uni-icons>
							</view>
							<!-- card的内容 -->
							<view class="card-body-item flex-start">
								<view class="body-item-title">{{ $t('smashRemoval.salesOrderNo') }}：</view>
								<view class="body-item-title specview">{{ x.salesOrders }}</view>
							</view>

							<view class="card-body-item">
								<view class="card-item-50">
									<text class="body-item-title">{{ $t('pageTranslation.totalQtyOfDelivery') }}：</text>
									{{ x.sumQty }}
								</view>
								<view class="card-item-50">
									<text class="body-item-title">{{ $t('smashRemoval.quantityDelivered') }}：</text>
									{{ x.sumScan }}
								</view>
							</view>
							<view class="card-body-item">
								<view class="card-item-50">
									<text class="body-item-title">{{ $t('smashRemoval.quantityNotDelivered') }}：</text>
									{{ x.sumQty - x.sumScan }}
								</view>
								<view class="card-item-50">
									<text class="body-item-title">{{ $t('distributedEnter.compiler') }}：</text>
									{{ x.createUserName }}
								</view>
							</view>
							<view class="card-body-item">
								<view class="card-item-50" style="width: 100%;">
									<text class="body-item-title">{{ $t('translationImage.documentationDate') }}：</text>
									{{ x.dnDate }}
								</view>
							</view>
							<view class="card-body-folding" v-show="data.list[i].openBool">
								<view class="gui-border-t" style="padding: 20rpx;"
									v-for="(k, index) in data.list[i].wmsDnDS" :key="index">
									<view class="card-body-item">
										<text class="body-item-title">{{ $t('distributedEnter.materialCode') }}：</text>
										{{ k.materialCode }}
									</view>
									<view class="card-body-item">
										<text class="body-item-title">{{ $t('distributedEnter.materialName') }}：</text>
										{{ k.materialName }}
									</view>
									<view class="card-body-item" v-if="k.spec && k.spec != ' '">
										<text class="body-item-title">{{ $t('distributedEnter.materialSpec') }}：</text>
										{{ k.spec }}
									</view>
									<view class="card-body-item">
										<text class="body-item-title">{{ $t('distributedEnter.uom') }}：</text>
										{{ k.saleUnit }}
									</view>
									<view class="card-body-item">
										<text class="body-item-title">{{ $t('materials.salesQuantity') }}：</text>
										{{ k.saleQty }}
									</view>
									<view class="card-body-item">
										<text class="body-item-title">{{ $t('smashRemoval.quantityDelivered') }}：</text>
										{{ k.scanQty }}
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-if="data.noticeHooks" class="noticeHooks">
						{{ data.list.length == 0 ? $t('stockManage.notAvailable') : $t('stockManage.without') }}
					</view>
				</scroll-view>
			</view>
			<calendarCustom ref="calendarCustomRef" @setTime="getTime" :selectTime="fromData.chooseTime">
			</calendarCustom>
		</template>
	</gui-page>
</template>
<script setup>
import searchCustom from '@/components/searchCustom';
import calendarCustom from '@/components/calendarCustom';
import {ref,reactive,onMounted,getCurrentInstance} from 'vue';
import {findDnHs,getSystemTime,selectStepoutH} from '@/api/api.js'
import { onShow } from '@dcloudio/uni-app';
import graceJS from "@/Grace6/js/grace.js";
import openView from '@/components/common/openView';
import superweiCombox from '@/components/superweiCombox';
import { navigate } from '@/utils/routerSkip.js';
import { useStore } from "vuex";
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_SEARCH, OPERATION_REASCT, OPERATION_LOOK, OPERATION_CHOOSE } from '@/constant/index.js'

//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

components: {
	superweiCombox,
		searchCustom,
		calendarCustom,
		openView
}
let store = useStore();
let calendarCustomRef = ref(null);
let fromData = reactive({
	chooseTime: [],
	selectIdText: t('smashRemoval.all'),//选中文字 默认全部
	selectId: '', //选中校验状态的id  全部传''
	receiptCode: '',
	page: 1,
	rows: 10,
});
let data = reactive({
	logoType: '',
	noticeHooks: false, //暂无相关数据标识
	list: [],
	selectList: [{
		id: '',
		name: t('smashRemoval.all')
	},
	{
		id: 'notSend',
		name: t('smashRemoval.unshipped')
	},
	{
		id: 'send',
		name: t('smashRemoval.delivered')
	}],
	listShow: [],
	operationType: '', // 操作日志操作类型
	page: 1,
	rows: 10,
});
const {
	ctx
} = getCurrentInstance();
onMounted(() => {
	getSystemTimeRequest()
})
onShow(() => {
	selectStepoutHFn(OPERATION_LOOK)
	// reset()
})
const getSystemTimeRequest = () => {
	store.dispatch('getSystemTimeRequest')
		.then(res => {
			console.log(res, '----param-----')
			if (res.length) {
				fromData.chooseTime.push(res)
			}
		})
}
//上拉加载
const scrolltolower = () => {
	if (!data.noticeHooks) {
		fromData.page++
		selectStepoutHFn(data.logoType)
	}
}
const selectStepoutHFn = (type) => {
	data.logoType = type
	let parmes = {
		dnDateStart: fromData.chooseTime[0] || '',
		dnDateEnd: fromData.chooseTime[1] ? fromData.chooseTime[1] : (fromData.chooseTime[0] || ''),
		dnStatus: fromData.selectId,
		dnCode: fromData.receiptCode,
		page: fromData.page,
		rows: fromData.rows,
		dnTypeCode: 'ts',
		isFs: '0',
		isDust: '0'
	}
	findDnHs(parmes).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '销售出库退货'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: type
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			data.noticeHooks = false
			if (fromData.page === 1) {
				if (res.data.rows.length == 0) {
					data.noticeHooks = true
				}
				data.list = res.data.rows
				data.list.forEach(element => {
					element.openBool = false
				});
			} else {
				let arr = res.data.rows
				arr.forEach(element => {
					element.openBool = false
				});
				data.list = [...data.list, ...arr]

			}
			data.noticeHooks = data.list.length >= res.data.total
		}
	})
}
const goPage = (x) => {
	if (x.dnStatus == 'notSend') {
		try {
			let inputParameter = x.salesOrders
			let returnParameter = '无'
			let operationPicture = '销售出库退货'
			let callInterface = '无'
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: '无',//返回状态
				errorMessage: '无',//错误信息
				isPass: 'Y',//是否成功 Y N
				operationType: OPERATION_CHOOSE
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {

		}
		console.log(222, x)
		navigate({
			url: `/pages/market/stockRemovalReturnDetail?id=${x.id}&salesOrders=${x.salesOrders}`
		})
	} else {
		uni.showToast({
			title: t('materials.deliveryHasBeen'),
			icon: 'none'
		})
	}
}
const search = () => {
	fromData.page = 1
	data.list = []
	data.noticeHooks = false
	selectStepoutHFn(OPERATION_SEARCH)
}
// 校验状态的选择事件
const select = (e) => {
	fromData.selectId = e.id;
}
const reset = () => {
	fromData.chooseTime = []
	fromData.page = 1
	fromData.receiptCode = ""
	fromData.selectId = ''
	fromData.selectIdText = ''
	data.noticeHooks = false
	selectStepoutHFn(OPERATION_REASCT)
}
const dateChoose = () => {
	calendarCustomRef.value.open();
}
const getTime = (times) => {
	fromData.chooseTime = times;
}
</script>
<style lang="scss" scoped>
.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}

.distributed {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.card-box {
	margin: 0 15rpx;
	box-shadow: 0rpx 0rpx 4rpx 0 #A5A5A5;
	background: #fff;
	padding: 30rpx;
	border-radius: 16rpx;
}

.card-item {
	line-height: 60rpx;
}

.card-bottom {
	margin-bottom: 30rpx;
}

.filtrate-item {
	display: flex;
	align-items: center;
	font-size: 32rpx;
	margin-bottom: 20rpx;

	.filtrate-itemLeft {
		height: 32rpx;
		line-height: 32rpx;
		width: 100rpx;
	}

	.filtrate-itemRight {
		border: 2rpx solid #A5A5A5;
		border-radius: 8rpx;
		overflow: hidden;
		line-height: 40rpx;
		width: 300rpx;
	}
}

// 展开按钮样式
.card-rotating-top {
	top: 100rpx !important;
}

.card-selectCustom {
	margin: 0.46875rem 0.625rem;
	box-shadow: 0 0 0.125rem 0 #a5a5a5;
	background: #fff;
	padding: 0.375rem 0.75rem;
	border-radius: 0.5rem;
	position: relative;
	height: 70rpx;
}

.flex-start {
	align-items: flex-start !important;
}

.specview {
	width: 70%;
	white-space: normal;
	word-break: break-all;
	color: #656565;
	flex: 1;
}

.card-body-item {
	margin-bottom: 10rpx;
}
</style>
